﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls; 

public partial class ContentMaster : System.Web.UI.MasterPage
{
    public string x; //get first querystring 
    public string y; //get second querystring
    string imgLocation; //get imagelocation for profile imagebutton

    protected void Page_Load(object sender, EventArgs e)
    {
        x = Request.QueryString["type"];  //request first querystring if it exist or else it will be null
        y = Request.QueryString["category"]; //request second querystring if it exist or else it will be null

        string strConnString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //connection string to database
        SqlConnection Connection = new SqlConnection(strConnString); //set connection string to sqlconnection
        Session["ConnDB"] = Connection;
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        string SqlQuery; //query for sql

        MembershipUser user = Membership.GetUser(); //get current user
        SqlQuery = "SELECT profileImage FROM userTable where userName ='" + user.UserName + "';"; //select the image in the usertable for the signed in user
        SqlCommand cmd = new SqlCommand(SqlQuery, Connection); //set command
        Connection.Open(); //open connection to database
        SqlDataReader Reader = cmd.ExecuteReader(); //execute command
        while (Reader.Read()) //while reader has content to read, do this
        {
            imgLocation = Reader[0] as string; //because the reader only get one entry, we can set it to Reader[0]
        }
        Connection.Close(); //close connection to database
        profilepicture.ImageUrl = imgLocation; //set image source to the profile imagebutton
    }

    public void logo_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Homescreen.aspx"); //if logo is clicked return user to homescreen
    }

    public void profile_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("AccountSettings.aspx"); //if profile imagebutton is clicked, go to account settings
    }
    protected void logout_Click1(object sender, EventArgs e)
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            Request.Cookies.Clear();
            Session.Abandon();
            FormsAuthentication.SignOut();
            Response.Redirect("Homescreen.aspx");
        }
    }
}
